package com.rednose.videoplayer.Dao;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.rednose.videoplayer.utils.UserDatabaseHelper;
import com.rednose.videoplayer.model.ListVideo;
import com.rednose.videoplayer.model.User;

import java.util.ArrayList;

public class ListVideoDao {
    private UserDatabaseHelper dbHelper;
    public ListVideoDao(Context context) {
        dbHelper = UserDatabaseHelper.getInstance(context);
    }
    public boolean addListVideo(String mediaID, int userID, int position) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("mediaID", mediaID);
        values.put("userID", userID);
        values.put("position", position);
        long result = db.insert("ListVideo", null, values);
        db.close();
        return result != -1;
    }
    public boolean addListVideo(ListVideo listVideo) {
        return addListVideo(listVideo.getMediaID(), listVideo.getUserID(), listVideo.getPosition());
    }
    @SuppressLint("Range")
    public ArrayList<ListVideo> getListVideoByUser(User user) {
        if (user == null) {
            throw new IllegalArgumentException("User object cannot be null");
        }
        int userID = user.getUserID();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query("ListVideo", null, "userID = ?", new String[]{String.valueOf(userID)}, null, null, null);
        ArrayList<ListVideo> listVideos = new ArrayList<>();
        if (cursor != null && cursor.moveToFirst()) {
            do {
                String mediaID = cursor.getString(cursor.getColumnIndex("mediaID"));
                int position = cursor.getInt(cursor.getColumnIndex("position"));
                listVideos.add(new ListVideo(mediaID, userID, position));
            } while (cursor.moveToNext());
            cursor.close();
        }
        db.close();
        return listVideos;
    }
    @SuppressLint("Range")
    public int getPosition(int mediaID, int userID) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query("ListVideo", null, "mediaID = ? AND userID = ?", new String[]{String.valueOf(mediaID), String.valueOf(userID)}, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            int position = cursor.getInt(cursor.getColumnIndex("position"));
            cursor.close();
            db.close();
            return position;
        }
        if (cursor != null) {
            cursor.close();
        }
        db.close();
        return -1;
    }

    //查找用户一共有多少个视频，用来设置最新的position
    @SuppressLint("Range")
    public int getVideoCount(int userID) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query("ListVideo", null, "userID = ?", new String[]{String.valueOf(userID)}, null, null, null);
        int count = 0;
        if (cursor != null && cursor.moveToFirst()) {
            do {
                count++;
            } while (cursor.moveToNext());
            cursor.close();
        }
        db.close();
        return count;
    }
    public boolean updatePosition(int mediaID, int userID, int position) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("position", position);
        int result = db.update("ListVideo", values, "mediaID = ? AND userID = ?", new String[]{String.valueOf(mediaID), String.valueOf(userID)});
        db.close();
        return result > 0;
    }
    @SuppressLint("Range")
    public boolean deleteListVideo(String mediaID, int userID) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.beginTransaction();
        boolean success = false;
        try {
            // 删除指定的记录
            int result = db.delete("ListVideo", "mediaID = ? AND userID = ?", new String[]{mediaID, String.valueOf(userID)});

            if (result > 0) {
                // 查询删除后的所有记录并按 position 排序
                Cursor cursor = db.rawQuery("SELECT mediaID, userID FROM ListVideo WHERE userID = ? ORDER BY position", new String[]{String.valueOf(userID)});

                // 更新所有记录的 position
                int newPosition = 1;
                while (cursor.moveToNext()) {
                    int currentMediaID = cursor.getInt(cursor.getColumnIndex("mediaID"));
                    int currentUserID = cursor.getInt(cursor.getColumnIndex("userID"));

                    ContentValues values = new ContentValues();
                    values.put("position", newPosition);

                    db.update("ListVideo", values, "mediaID = ? AND userID = ?", new String[]{String.valueOf(currentMediaID), String.valueOf(currentUserID)});
                    newPosition++;
                }
                cursor.close();
                success = true;
            }

            db.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.endTransaction();
            db.close();
        }
        return success;
    }

}
